import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.5
import TransferSocket 1.0
import TcpManager 1.0

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    property TransferSocket serverSocket;
    property TransferSocket clientSocket;
//    property TcpManager tcpManager;

    function connectComplete() {

    }

    Column {
        Button {
            id: btn0
            width: 100
            height: 100
            text: qsTr("启动服务器")
            onClicked: {
                TcpManager.listen()
            }
        }

        Button {
            id: btn2
            width: 100
            height: 100
            text: qsTr("连接服务器")
            onClicked: {
                TcpManager.connectToHost("test socket", "127.0.0.1", 43800)
            }
        }

        Button {
            id: btn3
            width: 100
            height: 100
            text: qsTr("客户端发送")
            onClicked: {
                clientSocket.writeToSocket("Hello world")
            }
        }

        Button {
            id: btn4
            width: 100
            height: 100
            text: qsTr("断开Tcp连接")
            onClicked: {
                clientSocket.disconnectFromHost();
            }
        }
    }


    Connections {
        target: TcpManager
        function onNewConnection(cppSocket) {
            console.info("server socket");
            serverSocket = cppSocket;
        }
        function onConnectToHostSuccess(cppSocket) {
            console.info("client socket");
            clientSocket = cppSocket;
        }
    }
}
